package com.woniu.shirocore;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.env.BasicIniEnvironment;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @author:陈涵
 * @Do:登录认证基本shiro入门ini校验demo
 * @date: 2022/9/5 15:07
 */
public class LoginAuthBasicDemo {
    public static void main(String[] args) {
        //1.读取配置文件
        BasicIniEnvironment basicIniEnvironment = new BasicIniEnvironment("classpath:ini/shiro-auth.ini");
        //创建securityManager,中央大脑
        SecurityManager securityManager = basicIniEnvironment.getSecurityManager();
        //使用工具类，该工具类必须注入securityManager
        SecurityUtils.setSecurityManager(securityManager);
        //通过工具类创建subject
        Subject subject = SecurityUtils.getSubject();
        //模拟前端传入后端的账号，通过login方法，交给SecurityManager帮助我去检查是否正确
        subject.login(new UsernamePasswordToken("chenxian","654321"));
        //如果登录成功,isAuthenticated() 过去式，true表示登录过了。
        System.out.println(subject.getPrincipal()+"登录成功："+subject.isAuthenticated());
    }
}
